
package rds;

import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huaweicloud.saaslanding.basic.exception.BizException;
import com.huaweicloud.saaslanding.basic.utils.CheckNullUtil;
import com.huaweicloud.saaslanding.dao.common.entity.app.AppDeployment;
import com.huaweicloud.saaslanding.dao.common.entity.app.AppTemplate;
import com.huaweicloud.saaslanding.dao.common.entity.tenant.TenantApp;
import com.huaweicloud.saaslanding.dao.common.repository.app.AppDeploymentBaseMapper;
import com.huaweicloud.saaslanding.dao.common.repository.app.AppTemplateBaseMapper;
import com.huaweicloud.saaslanding.dao.common.repository.tenant.TenantAppBaseMapper;
import com.huaweicloud.saaslanding.install.SaaslandingInstallApplication;
import com.huaweicloud.saaslanding.install.applicationservice.IdaasService;
import com.huaweicloud.saaslanding.install.applicationservice.ObsService;
import com.huaweicloud.saaslanding.install.applicationservice.RdsService;
import com.huaweicloud.saaslanding.install.applicationservice.RedisService;

import jakarta.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * 功能描述
 *
 * @since 2022-03-24
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SaaslandingInstallApplication.class, webEnvironment = RANDOM_PORT)
public class RdsTest {
    private RdsService rdsApi;

    @Autowired
    public void setRdsApi(RdsService rdsApi) {
        this.rdsApi = rdsApi;
    }

    @Resource
    AppTemplateBaseMapper appTemplateBaseMapper;

    @Resource
    AppDeploymentBaseMapper appDeploymentBaseMapper;

    @Resource
    TenantAppBaseMapper tenantAppBaseMapper;

    @Resource
    private RedisService redisService;

    @Resource
    private ObsService obsService;

    @Resource
    private IdaasService idaasService;

    @Test
    public void initDataBase() {
        rdsApi.initDataBase();
    }

    @Test
    public void initIdaasInfo() {
        idaasService.initIdaasInfo();
    }

    @Test
    public void initObs() {
        obsService.initObs();
    }

    @Test
    public void testRedisDel() {
        redisService.deleteRedis();
    }

    @Test
    public void rollbackDataBase() {
        rdsApi.rollbackDataBase();
    }

    private Long getAppId() {
        QueryWrapper<AppTemplate> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id").eq("is_del", 0).eq("type", 1);
        AppTemplate appTemplate = appTemplateBaseMapper.selectOne(queryWrapper);
        if (CheckNullUtil.checkObjAllFieldsIsNull(appTemplate)) {
            throw new BizException("未找到对应的应用模板");
        }
        return appTemplate.getId();
    }

    private long getRdsId(Long appId) {

        QueryWrapper<AppDeployment> deploymentInfoQueryWrapper = new QueryWrapper<>();
        deploymentInfoQueryWrapper.eq("app_id", appId).eq("is_del", 0);
        AppDeployment appDeployment = appDeploymentBaseMapper.selectOne(deploymentInfoQueryWrapper);
        if (CheckNullUtil.checkObjAllFieldsIsNull(appDeployment)) {
            throw new BizException("未找到对应的数据源信息");
        }
        return appDeployment.getRdsId();
    }

    private void initTenantApp(Long subAppId, Long tenantId) {
        TenantApp hslTenantApp = new TenantApp();

        long appId = getAppId();
        long rdsId = getRdsId(appId);
        hslTenantApp.setAppId(subAppId)
            .setTenantAppCode("ddd")
            .setTenantCode("ddd")
            .setTenantId(tenantId)
            .setRdsId(rdsId);
        tenantAppBaseMapper.insert(hslTenantApp);
    }

    @Test
    public void initTenantAppTest() {
        initTenantApp(222L, 66L);
    }

}
